<template>
  <!-- 2023年4月3日 孔德嘉 DIY 商品查询页面、全查页面1-->
  <div style="background: #fff">
    <!-- 搜索 -->
    <div class="query">
      <!-- 搜索框 -->
      <div class="query_box">
        <div class="textInputWrapper">
          <input
            placeholder="找到你所爱"
            type="text"
            class="textInput"
            v-model="queryDiyList.queryDiyName"
          />
        </div>
      </div>
      <!-- 搜索按钮 -->
      <div class="query_but">
        <button @click="toQuery">搜索</button>
      </div>
    </div>
    <!-- 中间 -->
    <div class="center">
      <!-- 条件们！ -->
      <el-card class="box-card" shadow="hover">
        <!-- 左侧标题 -->
        <div class="query_menu">
          <p
            class="classify_menu"
            @mouseover="showClassifyList = true"
            @mouseleave="showClassifyList = false"
          >
            所有分类
          </p>
          <p>价格区间</p>
        </div>
        <!-- 右侧内容 -->
        <div class="query_menu_content">
          <!-- 类型-->
          <p>
            <span
              @click="chooseType(0)"
              :class="[queryDiyList.typeId === 0 ? 'is_active' : '']"
              >全部</span
            >
            <span
              v-for="(item, index) in classifyList"
              :key="index"
              :class="[queryDiyList.typeId == item.id ? 'is_active' : '']"
              @click="chooseType(item.id)"
              >{{ item.typeName }}</span
            >
          </p>
          <!-- 价格 -->
          <p>
            <span
              @click="choosePrice(1)"
              :class="[
                queryDiyList.minPrice === undefined &&
                queryDiyList.maxPrice === undefined
                  ? 'is_active'
                  : '',
              ]"
              >全部</span
            >
            <span
              v-for="(item, index) in priceRangeList"
              :key="index"
              :class="[
                queryDiyList.minPrice === item.minPrice &&
                queryDiyList.maxPrice === item.maxPrice
                  ? 'is_active'
                  : '',
              ]"
              @click="choosePrice(item)"
              >{{ item.minPrice == undefined ? "0" : item.minPrice
              }}{{
                item.maxPrice == undefined ? "到以上" : " - " + item.maxPrice
              }}</span
            >
            <!-- 输入价格期间 -->
            <input
              placeholder=""
              type="text"
              name="text"
              class="input"
              v-model="queryDiyList.minPrice"
            />
            <i>—</i>
            <input
              placeholder=""
              type="text"
              name="text"
              class="input"
              v-model="queryDiyList.maxPrice"
            />
            <button class="isOk" @click="choosePrice()">确定</button>
          </p>
          <!-- 类型弹窗 -->
          <div
            class="classifyHover"
            v-show="showClassifyList == true"
            @mouseover="showClassifyList = true"
            @mouseleave="showClassifyList = false"
          >
            <div
              v-for="(item, index) in classifyList"
              @click="chooseType(item.id)"
              :key="index"
              class="text item classifyCard"
              style="
                width: 12%;
                text-align: center;
                display: inline-block;
                max-height: 130px;
                height: 130px;
                min-height: 12%;
              "
              :style="{ marginRight: (index + 1) % 6 != 0 ? '5%' : '0' }"
            >
              <el-image
                :src="
                  $imageUrl+'?id='+
                  item.imageId
                "
                class="image"
              >
                <div slot="placeholder" style="width: 100%">
                  <Loading></Loading>
                </div>
              </el-image>
              <p
                style="
                  margin: 0;
                  text-align: center;
                  font-weight: bold;
                  font-size: 14px;
                  overflow: hidden;
                  height: 30px;
                  line-height: 30px;
                "
              >
                {{ item.typeName }}
              </p>
            </div>
          </div>
        </div>
      </el-card>
      <!-- 排序 -->
      <el-row :gutter="20" style="margin-top: 30px" class="sort">
        <el-col :span="5" :offset="19"
          ><el-button style="border: 2px solid #000000"
            >排序依据
            <el-select v-model="queryDiyList.orderValue" placeholder="请选择">
              <el-option
                v-for="(item, index) in orderOptions"
                :key="index"
                :label="item.label"
                :value="item.value"
              >
              </el-option> </el-select></el-button
        ></el-col>
      </el-row>
      <!-- 商品卡片 -->
      <el-row style="margin-top: 30px" :gutter="120" class="diy_card">
        <el-col
          :span="6"
          v-for="(item, index) in diyShopList"
          :key="index"
          style="margin-bottom: 30px; height: 420px"
        >
          <el-card :body-style="{ padding: '0px' }" class="diy_card_child">
            <!-- 主图 -->
            <img
              @click="toDIYView(item.id)"
              :src="
                $imageUrl+'?id=' +
                item.diy_image_id
              "
              class="image1"
            />
            <div style="padding: 8px 14px 12px 16px">
              <p class="card_top">
                <!-- 价格 -->
                <span class="card_price"> ￥{{ item.price }} </span>
                <!-- 商品销量 -->
                <span class="card_volume">销量{{ item.sales_volume }}</span>
              </p>
              <!-- 商品名称 -->
              <p class="card_name" @click="toDIYView(item.id)">
                {{ item.diy_name }}
              </p>
              <p class="card_bot">
                <span class="card_type">{{ item.type_sname }}</span>
                <img class="card_star" src="../../assets/image/收藏.png" />
              </p>
            </div>
          </el-card>
        </el-col>
      </el-row>
      <div class="page">
        <el-pagination
          @current-change="upPageNum"
          :current-page.sync="currentPage4"
          :page-size="pageRequest.pageSize"
          layout="total,prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import Loading from "@/components/diy/Loading";
export default {
  name: "DIYShops",
  data() {
    return {
      // 分页显示第几页
      currentPage4: 1, 
      pageRequest: {
        // 分页请求参数
        pageSize: 48, // 每页条数
        pageNum: 1, // 当前页
      },
      total: 0, // 分页总条数
      //查询条件
      queryDiyList: {
         // 分类模块跳转id
        //类型id
        typeId: this.$route.query.typeId==null?0:this.$route.query.typeId,
        //模糊查diy商品名称
        queryDiyName: "",
        // 最低价格
        minPrice: undefined,
        // 最高价格
        maxPrice: undefined,
        //排序条件
        orderValue: "diy.create_time desc",
      },
      // 分类列表
      showClassifyList: false,
      // 价格区间列表
      priceRangeList: [
        {
          id: 1,
          minPrice: undefined,
          maxPrice: 100,
        },
        {
          id: 2,
          minPrice: 100,
          maxPrice: 300,
        },
        {
          id: 3,
          minPrice: 300,
          maxPrice: 500,
        },
        {
          id: 4,
          minPrice: 500,
          maxPrice: undefined,
        },
      ],
      //Diy商品列表
      diyShopList: [],
      //排序
      orderOptions: [
        {
          value: "diy.create_time desc",
          label: "相关性",
        },
        {
          value: "price asc",
          label: "价格升序",
        },
        {
          value: "price desc",
          label: "价格降序",
        },
        {
          value: "diy_grade desc",
          label: "好评",
        },
        {
          value: "sales_volume desc",
          label: "销量",
        },
      ],
      //分类
      classifyList: [],
      // 搜索框
      input: "",
    };
  },
  components: { Loading },
  mounted() {
    //加载DIY分类
    this.getHandworkDiyTypeList();
    //加载DIY商品列表
    this.getHandworkDiyListByPageParams();
  },
  watch: {
    "queryDiyList.orderValue": {
      handler(newValue, oldValue) {
        // 判断
        if (newValue != oldValue) {
          //查询
          this.getHandworkDiyListByPageParams();
        }
      },
    },
  },
  methods: {
    //改变页码
    upPageNum(val) {
      this.pageRequest.pageNum = val;
      this.getHandworkDiyListByPageParams();
    },
    //点击查询
    toQuery() {
      let name = this.queryDiyList.queryDiyName;
      // console.log(name);
      //清空查询条件
      (this.queryDiyList = {
        //类型id
        typeId: 0,
        //模糊查diy商品名称
        queryDiyName: "",
        // 最低价格
        minPrice: undefined,
        // 最高价格
        maxPrice: undefined,
        //排序条件
        orderValue: "RAND()",
      }),
        //赋值
        (this.queryDiyList.queryDiyName = name);
      this.getHandworkDiyListByPageParams();
    },
    // 选择 分类
    chooseType(typeId) {
      // console.log(typeId);
      //判断与之前的值是否一样
      if (this.queryDiyList.typeId != typeId) {
        //赋值给查询条件
        this.queryDiyList.typeId = typeId;
        //全查
        this.getHandworkDiyListByPageParams();
      }
    },
    // 根据商品名称 分类 价格 排序查询
    getHandworkDiyListByPageParams() {
      this.$axios
        .get("/handworkDiy/getHandworkDiyListByPageParams", {
          params: {
            //查询条件
            //类型id
            typeId: this.queryDiyList.typeId,
            //模糊查diy商品名称
            queryDiyName: this.queryDiyList.queryDiyName,
            // 最低价格
            minPrice: this.queryDiyList.minPrice,
            // 最高价格
            maxPrice: this.queryDiyList.maxPrice,
            //排序条件
            orderValue: this.queryDiyList.orderValue,
            //分页条件
            pageSize: this.pageRequest.pageSize,
            pageNum: this.pageRequest.pageNum,
          },
        })
        .then((res) => {
          // 查询DIY的类型列表
          this.diyShopList = res.data.data.content;
          //页数
          this.total = res.data.data.totalSize;
        })
        .catch(() => {
          this.$message.error("获取Diy商品失败！");
        });
    },
    // 选择 价格区间
    choosePrice(item) {
      //选择模块
      if (item != null) {
        // 判断值是否变化
        if (
          this.queryDiyList.minPrice != item.minPrice ||
          this.queryDiyList.maxPrice != item.maxPrice
        ) {
          //赋值给查询条件
          this.queryDiyList.minPrice = item.minPrice;
          this.queryDiyList.maxPrice = item.maxPrice;
          //全查
          this.getHandworkDiyListByPageParams();
        }
      } //全查
      else if (item == 1) {
        this.queryDiyList.minPrice = undefined;
        this.queryDiyList.maxPrice = undefined;
      } //自己输入
      else {
        //全查
        this.getHandworkDiyListByPageParams();
      }
      //页面给空值
      if (this.queryDiyList.minPrice == 0) {
        this.queryDiyList.minPrice = undefined;
      }
      if (this.queryDiyList.maxPrice == 0) {
        this.queryDiyList.maxPrice = undefined;
      }
    },
    // 查询DIY的类型列表
    getHandworkDiyTypeList() {
      this.$axios
        .get("/handworkDiy/getHandworkDiyTypeList")
        .then((res) => {
          // 查询DIY的类型列表
          this.classifyList = res.data.data;
        })
        .catch(() => {
          this.$message.error("获取DIY分类失败！");
        });
    },
    // 跳转到DIY商品详情页面
    toDIYView(id) {
      this.$router.push({
        path: "/DIYView",
        query: { diyId: id },
      });
    },
  },
};
</script>

<style scoped>
::v-deep .sort .el-input__inner {
  background: red;
  background: transparent;
  border: none;
}
.center {
  width: 80%;
  margin: 0 auto;
}
.image {
  width: 70%;
  min-width: 70%;
  aspect-ratio: 1/1;
  display: inline-block;
  border-radius: 4px;
}
/*隐藏的分类列表*/
.classifyHover {
  position: absolute;
  top: 0;
  left: -65px;
  padding-left: 65px;
  background: #fff;
  width: 100%;
}

.classifyCard:hover {
  transform: scale(1.1);
  transition: 0.4s;
}
/*排序*/
.el-button {
  border-radius: 50px;
  padding: 0 10px 0 14px;
}
.el-input {
  width: 100%;
}
.el-select {
  width: 130px;
}
/*商品 列表*/
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image1 {
  width: 100%;
  aspect-ratio: 1/1;
  display: block;
  cursor: pointer;
}
::v-deep.el-button:focus,
::v-deep .el-button:hover {
  color: #333;
  border-color: #6262622c;
  background-color: #9797970a;
}
::v-deep.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #333;
  color: #fff;
}
::v-deep.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #333;
  color: #fff;
}
::v-deep .el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #333;
}
::v-deep.el-select-dropdown__item.selected {
  color: #333;
  font-weight: 700;
}
/*搜索*/
.textInputWrapper {
  position: relative;
  width: calc(100% - 10px);
  --accent-color: #fff200;
}

.textInputWrapper:before {
  transition: border-bottom-color 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
  border-bottom: 1px solid rgba(0, 0, 0, 0.42);
}

.textInputWrapper:before,
.textInputWrapper:after {
  content: "";
  left: 0;
  right: 0;
  position: absolute;
  pointer-events: none;
  bottom: -1px;
  z-index: 4;
  width: 100%;
}

.textInputWrapper:focus-within:before {
  border-bottom: 1px solid var(--accent-color);
}

.textInputWrapper:before {
  transition: border-bottom-color 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
  border-bottom: 1px solid rgba(0, 0, 0, 0.42);
}

.textInputWrapper:focus-within:before {
  border-bottom: 1px solid var(--accent-color);
  transform: scaleX(1);
}

.textInputWrapper:focus-within:after {
  border-bottom: 2px solid var(--accent-color);
  transform: scaleX(1);
}

.textInputWrapper:after {
  content: "";
  transform: scaleX(0);
  transition: transform 250ms cubic-bezier(0, 0, 0.2, 1) 0ms;
  will-change: transform;
  border-bottom: 2px solid var(--accent-color);
  border-bottom-color: var(--accent-color);
}

.textInput::placeholder {
  transition: opacity 250ms cubic-bezier(0, 0, 0.2, 1) 0ms;
  opacity: 1;
  user-select: none;
  color: rgba(255, 255, 255, 0.582);
}

.textInputWrapper .textInput {
  border-radius: 5px 5px 0px 0px;
  box-shadow: 0px 2px 5px rgb(35 35 35 / 30%);
  max-height: 36px;
  background-color: #252525;
  transition-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1);
  transition-duration: 200ms;
  transition-property: background-color;
  color: #e8e8e8;
  font-size: 14px;
  font-weight: 500;
  padding: 12px;
  width: calc(100% - 24px);
  border-left: none;
  border-bottom: none;
  border-right: none;
}

.textInputWrapper .textInput:focus,
.textInputWrapper .textInput:active {
  outline: none;
}

.textInputWrapper:focus-within .textInput,
.textInputWrapper .textInput:focus,
.textInputWrapper .textInput:active {
  background-color: #353535;
}

.textInputWrapper:focus-within .textInput::placeholder {
  opacity: 0;
}
/*搜索*/
.query {
  padding: 20px 0;
  width: 45%;
  margin: 0 auto;
}
.query > div {
  display: inline-block;
}
.query_box {
  width: 77%;
  margin-right: 10%;
}
.query_but {
  width: 13%;
  position: relative;
  top: 2px;
}
.query_but button {
  appearance: none;
  background-color: transparent;
  border: 0.125em solid #252525;
  border-radius: 0.9375em;
  box-sizing: border-box;
  color: #3b3b3b;
  cursor: pointer;
  display: inline-block;
  font-family: Roobert, -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica,
    Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  font-size: 16px;
  font-weight: 600;
  line-height: normal;
  margin: 0;
  min-height: 42px;
  min-width: 0;
  outline: none;
  padding: 0 1.3em;
  text-align: center;
  text-decoration: none;
  transition: all 300ms cubic-bezier(0.23, 1, 0.32, 1);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  will-change: transform;
}

.query_but button:disabled {
  pointer-events: none;
}

.query_but button:hover {
  color: #fff200;
  background-color: #252525;
  transform: translateY(-2px);
}

.query_but button:active {
  box-shadow: none;
  transform: translateY(0);
}
/*选项*/
.box-card {
  overflow: hidden;
  border: 3px solid #333;
  border-radius: 30px;
}
::v-deep .el-card__body {
  overflow: hidden;
}
/*分类查询*/
.query_menu {
  float: left;
  width: 15%;
  margin-right: 5%;
  color: #fff200;
  font-weight: 700;
  font-size: 16px;
  height: 100%;
}
.query_menu > p {
  text-align: center;
  background: #333;
  height: 50px;
  line-height: 50px;
  margin: 0;
}
.query_menu_content p:nth-child(1),
.query_menu > p:nth-child(1) {
  margin-bottom: 10px;
}
/*具体内容*/
.query_menu_content {
  float: left;
  width: 80%;
  position: relative;
}
.query_menu_content p {
  text-align: left;
  font-size: 14px;
  height: 50px;
  line-height: 50px;
  margin: 0;
}
.query_menu_content i {
  padding: 0 10px;
}
/*类型名称*/
.query_menu_content span {
  padding: 0 10px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  height: 30px;
  margin: 15px 0;
  line-height: 30px;
  display: inline-block;
}
.query_menu_content .is_active {
  background: #000000;
  color: #fff200;
}
.query_menu_content span:hover:not(.is_active) {
  background: #000000;
  opacity: 0.4;
  color: #fff200;
}
/*价格输入框*/
.query_menu_content .input {
  width: 50px;
  max-width: 100px;
  height: 24px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1.5px solid lightgrey;
  outline: none;
  transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);
  box-shadow: 0px 0px 20px -18px;
}

.query_menu_content .input:hover {
  border: 2px solid lightgrey;
  box-shadow: 0px 0px 20px -17px;
}

.query_menu_content .input:active {
  transform: scale(0.95);
}

.query_menu_content .input:focus {
  border: 2px solid grey;
}
/*确定按钮*/
.isOk {
  margin-left: 20px;
  appearance: none;
  box-sizing: border-box;
  color: #24292e;
  cursor: pointer;
  display: inline-block;
  font-family: -apple-system, system-ui, "Segoe UI", Helvetica, Arial,
    sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  font-size: 14px;
  font-weight: 500;
  line-height: 20px;
  list-style: none;
  padding: px 16px;
  position: relative;
  transition: background-color 0.2s cubic-bezier(0.3, 0, 0.5, 1);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  vertical-align: middle;
  white-space: nowrap;
  word-wrap: break-word;
  border-radius: 12px;
  border: 1.5px solid lightgrey;
  box-shadow: 0px 0px 20px -18px;
  background: #fff;
}

.isOk:hover {
  background-color: #f3f4f6;
  text-decoration: none;
  transition-duration: 0.1s;
}

.isOk:disabled {
  background-color: #fafbfc;
  border-color: rgba(27, 31, 35, 0.15);
  color: #959da5;
  cursor: default;
}

.isOk:active {
  background-color: #edeff2;
  box-shadow: rgba(225, 228, 232, 0.2) 0 1px 0 inset;
  transition: none 0s;
}

.isOk:focus {
  outline: 1px transparent;
}

.isOk:before {
  display: none;
}

.isOk:-webkit-details-marker {
  display: none;
}
/*商品详情*/
.card_top {
  margin: 6px 0;
  height: 22px;
}
.card_top span {
  display: inline-block;
  vertical-align: middle;
  height: 22px;
  line-height: 22px;
}
.card_price {
  margin: 0 0;
  display: inline-block;
  color: #cc3124;
  font-weight: 600;
  font-size: 18px;
  font-family: arial;
  letter-spacing: 0.8px;
}
.card_type {
  font-size: 12px;
  color: #888;
}
.card_volume {
  font-size: 12px;
  color: #888;
  float: right;
  font-weight: 400;
}
.card_name {
  margin: 6px 0 0 0;
  font-weight: 700;
  height: 50px;
  line-height: 25px;
  font-size: 14px;
  overflow: hidden;
  cursor: pointer;
}
.card_star {
  width: 16px;
  height: 16px;
  float: right;
  margin-top: 4px;
}
.card_bot {
  height: 18px;
  line-height: 18px;
  margin: 0;
}
/*分页*/
.page {
  width: 60%;
  text-align: center;
  margin: 0 auto;
  margin-bottom: 30px;
}
/*分页*/
::v-deep .page .el-pager li.active {
  color: #000000;
  background: #fff200;
}
::v-deep .page .el-pager li:hover {
  color: #000000;
  background: #fffc9e;
}
::v-deep .page .el-pagination button:hover {
  color: #000000;
  background: #fffc9e;
}
::v-deep .page .el-input__inner:focus {
  border: 2px solid #fff200;
  outline: 0;
}
/*卡片鼠标移入*/
::v-deep .diy_card .el-card.is-always-shadow:hover {
  border: 2px solid #000000;
  border-radius: 10px;
  transform: scale(1.03);
}
::v-deep .el-range-editor.is-active,
::v-deep .el-range-editor.is-active:hover,
.el-select .el-input.is-focus .el-input__inner {
  border: none;
}
</style>